package mapper;

import server.HttpServlet;
import server.Request;

import java.util.HashMap;
import java.util.List;
import org.dom4j.Element;

public class MappedHost {

    private HashMap<String,MappedContext> mappedContexts = new HashMap<>();		// "localhost" --> mappedHost

    public HttpServlet findFirstServlet(Request request) {
        String host = request.getHost();
        if (mappedContexts.containsKey(host)) {
            return mappedContexts.get(host).findFirstServlet(request);
        }

        return null;
    }

    public MappedHost(List<Element> hostElements) {
        for (Element hostElement: hostElements) {
            String hostName = hostElement.attributeValue("name");
            String appBase = hostElement.attributeValue("appBase");

            if (hostName != null && hostName.length() > 0 && appBase != null && appBase.length() > 0) {
                MappedContext mappedContext = new MappedContext(appBase);
                if (mappedContext != null) {
                    mappedContexts.put(hostName, mappedContext);
                }

            }

        }
    }


}
